#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>

//library function
void work1()
{
    FILE* pfile = fopen("bite" , "w+");
    const char* st = "linux so easy!!!";
    size_t len  = strlen(st);
    fwrite(st , len,1 , pfile);
    fclose(pfile);

    FILE* pfile1 = fopen("bite" , "r");

    char buffer[256];
    size_t num =  fread(buffer , 1 , strlen(st) , pfile1);
    
    buffer[num] = '\0';
    printf("%s" , buffer);
    fclose(pfile1);
}
//system call function
void work2()
{
    int fd = open("bite" , O_CREAT | O_TRUNC | O_WRONLY , 0666);
    const char* content = "i like linux!!!";
    write(fd , content , strlen(content));

    int fd1 = open("bite" , O_RDONLY );
    char buffer[256];
    read(fd1 , buffer , strlen(content));

    printf("%s" , buffer);
    close(fd);
    close(fd1);
}
//the simultaneous read between two preocess on the identical file
void work3()
{
    //向文件中写入内容
    int fd = open("bite" , O_CREAT | O_TRUNC | O_WRONLY , 0666);
    const char* content = "i like linux!!!";
    write(fd , content , strlen(content));
    //循环的读取并打印文件内容
    int fd1 = open("bite" , O_RDONLY );
    char buffer[256];
    while(1)
    {
        read(fd1 , buffer , strlen(content));
        printf("%s" , buffer);
        fflush(stdout);
        sleep(1);
    }
}
int main()
{
    //work1()
    //work2();
    work3();
    return 0;
}
